package cn.sfa.service;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import microsoft.exchange.webservices.data.Appointment;
import microsoft.exchange.webservices.data.ConflictResolutionMode;
import microsoft.exchange.webservices.data.DeleteMode;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.ItemId;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.SendInvitationsMode;
import microsoft.exchange.webservices.data.WebCredentials;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Service
public class ExchangeAPIService {
	Logger log = LoggerFactory.getLogger(ExchangeAPIService.class);
 
	public ExchangeService StartExchange() {
		ExchangeService service = new ExchangeService(
				ExchangeVersion.Exchange2010);
		ExchangeCredentials credentials = new WebCredentials("baoql@it.hk",
				"abc123!@#");
		service.setCredentials(credentials);
		try {
			service.setUrl(new URI("http://192.168.1.55/EWS/Exchange.asmx"));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return service;
	}
	
	public String get(HttpServletResponse response,
			HttpServletRequest request) { 
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/exchangeapi";
	}

	/* 发送邮件 */ 
	public String create(HttpServletResponse response,
			HttpServletRequest request) {
		String recipient = request.getParameter("recipient");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		try {
			ExchangeService service = StartExchange();
			EmailMessage msg = new EmailMessage(service);
			msg.setSubject(title);
			msg.setBody(MessageBody.getMessageBodyFromText(content));
			msg.getToRecipients().add(recipient);
			msg.send();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/exchangeapi";
	}
	
	/*修改日程*/ 
	public void UpdateAppintment() {
		ExchangeService service = StartExchange();
		try {
			Appointment appointment = Appointment.bind(service, new ItemId("AAMkA="));
			appointment.setSubject("开会啦");
			appointment
					.setBody(MessageBody
							.getMessageBodyFromText("大家好，请及时参与会议。会议时间修改了。"));
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd hh:mm:ss");
			Date startDate = formatter.parse("2012-2-18 10:00:00");
			Date endDate = formatter.parse("2012-2-18 11:00:00");
			appointment.setStart(startDate);
			appointment.setEnd(endDate);
			appointment.getRequiredAttendees().add("baoql@it.hk"); //收件人
			appointment.getRequiredAttendees().add("yikun@it.hk");//收件人
			appointment.getOptionalAttendees().add("zhouyufan@it.hk");//抄送人
			appointment.update(ConflictResolutionMode.AlwaysOverwrite);

		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/*创建日程*/ 
	public void CreateAppintment() {
		ExchangeService service = StartExchange();
		try {
			Appointment appointment = new Appointment(service);
			appointment.setSubject("开会啦!");			
			appointment
					.setBody(MessageBody
							.getMessageBodyFromText("大家好，请及时参与会议。"));
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd hh:mm:ss");
			Date startDate = formatter.parse("2012-2-18 8:00:00");
			Date endDate = formatter.parse("2012-2-18 10:00:00");
			
			appointment.setStart(startDate);
			appointment.setEnd(endDate);
			appointment.getRequiredAttendees().add("baoql@it.hk"); //收件人
			appointment.getRequiredAttendees().add("yikun@it.hk");//收件人
			appointment.getOptionalAttendees().add("zhouyufan@it.hk");//抄送人
			appointment.save(SendInvitationsMode.SendToAllAndSaveCopy);

		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/*删除日程*/
	@RequestMapping("/deleteappointment")
	public String DeleteAppintment(HttpServletResponse response,
			HttpServletRequest request) {
		ExchangeService service = StartExchange();
		try {
			Appointment appointment = Appointment.bind(service, new ItemId("AAMkA="));
			appointment.delete(DeleteMode.MoveToDeletedItems);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/exchangeapi";
	}
	
	/*取消日程*/
	@RequestMapping("/cancelappointment")
	public String CancelAppintment(HttpServletResponse response,
			HttpServletRequest request) {
		ExchangeService service = StartExchange();
		try {
			Appointment appointment = Appointment.bind(service, new ItemId("AAMkA="));
			appointment.cancelMeeting();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/exchangeapi";
	}
	
	/*转发日程*/
	@RequestMapping("/forwardappointment")
	public String ForwardAppintment(HttpServletResponse response,
			HttpServletRequest request) {
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/exchangeapi";
	}

	
}
